<?php
	require("config.php");

	function writeGadgetPrefs($gadget, $moduleId)
	{

?>
<div class="meditbox">
			<input type="hidden" value="<?php echo $GLOBALS['gadgetFileName'];?>" name="gadget" id="gadgetUrl"/>
            <table border=0 cellpadding=0 cellspacing=0 width=95% align=center>
			
<?php
			$messageFile = ((string)$gadget->ModulePrefs->Locale['messages']);
			

			if ($messageFile != "")
			{
				$messageFile =  $messageFile;
				//echo $messageFile;
				if (file_exists($langDir .$messageFile))
				{
					$messages = simplexml_load_file($messageFile);
				} 
				else
					{
					exit("Can't open messages file: ".$langDir.$messageFile."place message bundle xml files in the /IGLocal/lang directory use relative urls in gadget xml source e.g. 'ALL_ALL.xml'");
				}
			}
	
				$i = 0;
				foreach ($gadget->UserPref as $up)
				{
					
					$name = ((string)$up['name']);
					$displayname = ((string)$up['display_name']);
					$value = $up['default_value'];
					$required = ((string)$up['required']) == "true" ? true : false;

					//load separate gadget file if specified in gadget declaration

					if ((string)$up['datatype'] == "hidden")
					{
						if ($value != "")
						{
							switch($name)
							{
								case 'jsGadgetContentFile' :
										$GLOBALS['gadgetJs'] = loadFile($GLOBALS['jsDir'].$value);
									break;

								case 'cssGadgetContentFile' :
										$GLOBALS['gadgetCss'] = loadFile($GLOBALS['cssDir'].$value);
									break;

								case 'htmlGadgetContentFile' :
										$GLOBALS['gadgetHtml'] = loadFile($GLOBALS['htmlDir'].$value);
									break;
							}
						}
					}
					else
					{
						//Override default UP value with one specified in query string
						@$qValue = $_GET["up_" . $name];
						if ((isset($qValue)) && ($qValue != ""))
						{
							$value = $qValue;
						}
						
						if ($messageFile != "")
						{
							//Replace message bundles inside userprefs
							foreach ($messages->msg as $message)
							{
								$displayname = str_replace("__MSG_".$message['name']."__", $message, $displayname);
								$value = str_replace("__MSG_".$message['name']."__", $message, $value);
							}
						}
?>
						<tr>
							<td colspan=1 align=right width="35%">
<?php 
								echo $displayname;
								if ($required == true)
								{
									echo '<font color="#ff0000"><sup>*</sup></font>';
								}
?>
							</td>
							<td colspan=2 nowrap align=left width="65%">&nbsp;
<?php

							switch((string) $up['datatype'])
							{
								// Get attributes as element indices
								case 'list':
?>
									<nobr>
										<input type="text" name="m_<?php echo $moduleId;?>_<?php echo $i;?>_up_<?php echo $name; ?>_val" value="" id="m_<?php echo $moduleId;?>_<?php echo $i;?>_val" maxlen="200" size="20"/>
										<input type="button" id="m_<?php echo $moduleId;?>_<?php echo $i;?>_add" onclick="m_<?php echo $moduleId;?>_<?php echo $i;?>_App.add();" value="Add"/>
										<input type="hidden" value="<?php echo $value;?>" name="up_<?php echo $name;?>" id="m_<?php echo $moduleId;?>_<?php echo $i;?>"/>
									</nobr>
								</td>
							</tr>
<?php
							$list = split("\|", $value);
							$items = count($list);
							if ($items > 0)
							{
?>
								<tr>
									<td>
									</td>
									<td>
										<div style="padding-top: 4px;" id="m_<?php echo $moduleId;?>_<?php echo $i;?>_disp">
											<font size="-1">
<?php
									for ($j = 0; $j < $items; $j++)
									{
										echo '<a style="margin: 1px 3px 0px 0px; float: left;" class="delbox" onclick="m_<?php echo $moduleId;?>_<?php echo $i;?>_App._del(1)" href="#"></a>';
										echo $list[$j];
										echo "<br/>";
?>
										
<?php
									}
?>
											</font>
										</div>
									</td>
								</tr>
<?php
							}
									
									break;
								
								case 'bool':
?>
									<input type=hidden id=m_<?php echo $moduleId;?>_<?php echo $i; ?> name=up_<?php echo $name?> value=1><input type=checkbox <?php echo ($value == "false") ? "UNCHECKED" : "CHECKED";  ?> onClick="_gel('m_<?php echo $moduleId;?>_<?php echo $i; ?>').value = this.checked ? '1' : '0';">									
<?php
									break;

								case 'enum':
?>
									<select style="width: 95%;" name="up_<?php echo $name;?>" id="m_<?php echo $moduleId;?>_<?php echo $i; ?>">
<?php
									foreach ($up->EnumValue as $enumval) 
									{
										//todo use a hashmap or something other than this horrific recursion
										$display = ((string)$enumval['display_value']);
										if ($display == "")
										{
											$display = ((string)$enumval['value']);
										}
											
										if ($messageFile != "")
										{
											foreach ($messages->msg as $message)
											{
												$display = str_replace("__MSG_".$message['name']."__", $message, $display);
											}
										}
										$selected = "";
										if (((string)$enumval['value']) == $value)
										{
											$selected = 'selected=""';
										}
?>									
										<option value="<?php echo $enumval['value'];?>" <?php echo $selected; ?>><?php echo $display;?></option>
<?php
							}
?>
									</select>
<?php
								break;
								default:
?>
									<input type="text" size="20" maxlen="200" id=m_<?php echo $moduleId;?>_<?php echo $i; ?> name=up_<?php echo $name?> value="<?php echo $value?>">
<?php

							}

							
?>
							</td>
						</tr>
<?php
					$i++;
					}
				}
?>
                
            </table>

            <table border=0 cellpadding=0 cellspacing=0 width=95% align=center>
                <tr>
                    <td colspan=2 nowrap align=left><br><a href=""><font size=-1 color=#7777cc></font></a></td>
                    <td colspan=1 align=right valign=bottom>
                        <input id=m_<?php echo $moduleId;?>_numfields type=hidden value=19>
						<input id=save_<?php echo $moduleId;?> class=submitbtn type=submit value="Save">
						<input type=button value="Cancel" onclick="return _cedit('0')">
                    </td>
                </tr>
            </table>
    </div>
<?php
	}
?>